package com.shuwei.wificollecttool;

import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.shuwei.location.wificollect.WifiCollectListener;
import com.shuwei.location.wificollect.WifiCollector;
import com.shuwei.location.wificollect.WifiItem;
import com.shuwei.wificollecttool.recognize.FindFingerPrint9Kind;

import java.util.List;

/**
 * Created by Halohoop on 2017/11/24.
 */

public class ArithmeticActivity extends AppCompatActivity implements View.OnClickListener,
        WifiCollectListener<WifiItem>,FindFingerPrint9Kind.LocationListener {

    private TextView tv_fun0;
    private TextView tv_fun1;
    private TextView tv_fun2;
    private TextView tv_fun3;
    private TextView tv_fun4;
    private TextView tv_fun5;
    private TextView tv_fun6;
//    private TextView tv_fun7;
    private TextView tv_fun8;
    private TextView tv_fun9;
    private Button btnRecog;
    private WifiCollector wifiCollector;
    private FindFingerPrint9Kind findFingerPrint;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_arithmetic);
        tv_fun0 = findViewById(R.id.tv_fun0);
        tv_fun1 = findViewById(R.id.tv_fun1);
        tv_fun2 = findViewById(R.id.tv_fun2);
        tv_fun3 = findViewById(R.id.tv_fun3);
        tv_fun4 = findViewById(R.id.tv_fun4);
        tv_fun5 = findViewById(R.id.tv_fun5);
        tv_fun6 = findViewById(R.id.tv_fun6);
//        tv_fun0 = findViewById(R.id.tv_fun7);
        tv_fun8 = findViewById(R.id.tv_fun8);
        tv_fun9 = findViewById(R.id.tv_fun9);
        btnRecog = findViewById(R.id.btn_recog);
        btnRecog.setOnClickListener(this);


        //设置回调
//设置转换器,转换成自己需要的bean
//                .setSortTarget(WifiCollector.Config.SORT_TARGET_RSSI)//设置排序
//                .setSortType(WifiCollector.Config.SORT_TYPE_Z2A_9TO0_STRONGTOWEAK)//设置排序
//                .setCount(15)//设置需要前多少位
        wifiCollector = new WifiCollector.Builder<WifiItem>(this)
                .setWifiCollectListener(this)//设置回调
                .setConverter(new WifiCollector.Converter<WifiItem>() {
                    @Override
                    public WifiItem convert(ScanResult scanResult) {
                        WifiItem wifiItem = new WifiItem();
                        wifiItem.setBssid(scanResult.BSSID);
                        wifiItem.setRssi(scanResult.level);
                        wifiItem.setSsid(scanResult.SSID);
                        return wifiItem;
                    }
                })//设置转换器,转换成自己需要的bean
//                .setSortTarget(WifiCollector.Config.SORT_TARGET_RSSI)//设置排序
//                .setSortType(WifiCollector.Config.SORT_TYPE_Z2A_9TO0_STRONGTOWEAK)//设置排序
//                .setCount(15)//设置需要前多少位
                .create();

        findFingerPrint = new FindFingerPrint9Kind();
        findFingerPrint.setLocationListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recog:
                btnRecog.setEnabled(false);
                wifiCollector.collect(null);
                break;
            default:
                break;
        }
    }

    @Override
    public void wifiCollectSuccess(List<WifiItem> wifiItemList) {
        findFingerPrint.locationHit(wifiItemList);
        btnRecog.setEnabled(true);
    }

    @Override
    public void wifiCollectFail(int type, String msg) {
        btnRecog.setEnabled(true);
        showToast(msg);
    }


    private Toast toast;

    public void showToast(String s){
        if (toast==null) {
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        }
        toast.setText(s);
        toast.show();
    }

    @Override
    public void locationHit(int funIndex, String msg, int hitIndex) {
        String roomDesc = getRoomDesc(hitIndex);
        switch (funIndex) {
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_0:
                tv_fun0.setText("方法1 进化曼哈顿距离:\n" + roomDesc);
                break;
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_1:
                tv_fun1.setText("方法2:\n" + roomDesc);
                break;
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_2:
                tv_fun2.setText("方法3 欧式距离:\n" + roomDesc);
                break;
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_3:
                tv_fun3.setText("方法4:\n" + roomDesc);
                break;
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_4:
                tv_fun4.setText("方法5:\n" + roomDesc);
                break;
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_5:
                tv_fun5.setText("方法6 consine相似度:\n" + roomDesc);
                break;
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_6:
                tv_fun6.setText("方法7 tanimoto系数:\n" + roomDesc);
                break;
//            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_7:
//                tv_fun7.setText("方法8 马氏距离:\n"+getRoomDesc(hitIndex));
//                break;
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_8:
                tv_fun8.setText("方法9 j&m 距离:\n" + roomDesc);
                break;
            case FindFingerPrint9Kind.LocationListener.FUN_INDEX_9:
                tv_fun9.setText("方法10 兰式距离:\n" + roomDesc);
                break;
            default:
                break;
        }
    }

    private String getRoomDesc(int hitIndex) {
        if (hitIndex >= 0 && hitIndex <= 4) {
            if (hitIndex == 3) {
                return "房间2门口";
            }
            return "房间2";
        } else if(hitIndex >= 5 && hitIndex <= 11){
            if (hitIndex == 11) {
                return "房间6门口";
            }
            return "房间6";
        } else if(hitIndex >= 12 && hitIndex <= 16){
            if (hitIndex == 16) {
                return "房间4门口";
            }
            return "房间4";
        } else if(hitIndex >= 17 && hitIndex <= 21){
            if (hitIndex == 21) {
                return "房间5门口";
            }
            return "房间5";
        } else if(hitIndex >= 22 && hitIndex <= 26){
            if (hitIndex == 26) {
                return "房间7门口";
            }
            return "房间7";
        } else if(hitIndex >= 27 && hitIndex <= 31){
            if (hitIndex == 31) {
                return "房间8门口";
            }
            return "房间8";
        } else if(hitIndex >= 32 && hitIndex <= 34){
            if (hitIndex == 34) {
                return "房间3门口";
            }
            return "房间3";
        } else if(hitIndex >= 35 && hitIndex <= 37){
            if (hitIndex == 37) {
                return "房间1门口";
            }
            return "房间1";
        }
        return "没有命中";
    }
}
